package com.atguigu.tingshu.search.api;

import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.query.search.AlbumIndexQuery;
import com.atguigu.tingshu.search.service.SearchService;
import com.atguigu.tingshu.vo.album.AlbumDetailInfoVo;
import com.atguigu.tingshu.vo.search.AlbumChannelVo;
import com.atguigu.tingshu.vo.search.AlbumInfoIndexVo;
import com.atguigu.tingshu.vo.search.AlbumSearchResponseVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "搜索专辑管理")
@RestController
@RequestMapping("api/search")
@Slf4j
@SuppressWarnings({ "all" })
public class SearchApiController {

    @Autowired
    private SearchService searchService;

    /**
     * 将指定专辑上架到索引库
     *
     * @param albumId 专辑ID
     * @return
     */
    @Operation(summary = "将指定专辑上架到索引库")
    @GetMapping("/albumInfo/upperAlbum/{albumId}")
    public Result upperAlbum(@PathVariable Long albumId) {
        searchService.upperAlbum(albumId);
        return Result.ok();
    }

    /**
     * 将指定专辑从索引库下架
     *
     * @param albumId 专辑ID
     * @return
     */
    @Operation(summary = "将指定专辑从索引库下架")
    @GetMapping("/albumInfo/lowerAlbum/{albumId}")
    public Result lowerAlbum(@PathVariable Long albumId) {
        searchService.lowerAlbum(albumId);
        return Result.ok();
    }

    /**
     * 专辑信息检索
     *
     * @param albumIndexQuery 专辑信息检索参数
     * @return
     */
    @Operation(summary = "专辑信息检索")
    @PostMapping("/albumInfo")
    public Result<AlbumSearchResponseVo> searchAlbumInfo(@RequestBody AlbumIndexQuery albumIndexQuery) {
        log.info("专辑信息检索参数：{}", albumIndexQuery);
        return Result.ok(searchService.searchAlbumInfo(albumIndexQuery));
    }

    /**
     * 查询热门专辑（按热度排序）
     *
     * @param category1Id 一级分类ID
     * @param pageNo      页码，默认1
     * @return
     */
    @Operation(summary = "查询热门专辑")
    @GetMapping("/albumInfo/channel/{category1Id}")
    public Result<List<AlbumChannelVo>> findHotAlbum(
            @PathVariable Long category1Id,
            @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo) {
        log.info("查询热门专辑 - category1Id: {}, pageNo: {}", category1Id, pageNo);
        return Result.ok(searchService.findHotAlbum(category1Id, pageNo));
    }

    /**
     * 专辑标题自动补全
     *
     * @param keyword 关键词
     * @return 匹配的专辑标题列表
     */
    @Operation(summary = "专辑标题自动补全")
    @GetMapping("/albumInfo/completeSuggest/{keyword}")
    public Result<List<String>> completeSuggest(@PathVariable String keyword) {
        log.info("专辑标题自动补全 - keyword: {}", keyword);
        return Result.ok(searchService.completeSuggest(keyword));
    }

    /**
     * 获取专辑详情
     *
     * @param albumId 专辑ID
     * @return 专辑详情
     */
    @Operation(summary = "获取专辑详情")
    @GetMapping("/albumInfo/{albumId}")
    public Result<AlbumDetailInfoVo> searchAlbumInfoDetail(@PathVariable Long albumId) {
        log.info("获取专辑详情 - albumId: {}", albumId);
        return Result.ok(searchService.searchAlbumInfoDetail(albumId));
    }

    /**
     * 查询专辑排行榜
     *
     * @param category1Id 一级分类ID
     * @param dimension   排行维度：hotScore-热度、playStatNum-播放量、subscribeStatNum-订阅量、buyStatNum-购买量、albumCommentStatNum-评论数
     * @return 排行榜专辑列表（Top 100）
     */
    @Operation(summary = "查询专辑排行榜")
    @GetMapping("/albumInfo/findRankingList/{category1Id}/{dimension}")
    public Result<List<AlbumInfoIndexVo>> findRankingList(
            @PathVariable Long category1Id,
            @PathVariable String dimension) {
        log.info("查询专辑排行榜 - category1Id: {}, dimension: {}", category1Id, dimension);
        return Result.ok(searchService.findRankingList(category1Id, dimension));
    }
}
